package com.suxin.thread.manager.pool;


import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;

/**
 * @author Tang
 * @classname AbstractQueueRunnable
 * @description [  ]
 * @date 2022/10/17 15:38
 */
@Slf4j
public abstract class AbstractQueueRunnable<T> extends AbstractSyncRunnable {

    private BlockingQueue<T> blockQueue;

    public AbstractQueueRunnable(Executor executor) {
        super(executor);
        initBlockQueue();
    }

    public T add(T data) {
        blockQueue.add(data);
        return data;
    }

    public void cleanAll() {
        blockQueue.clear();
    }

    @Override
    protected boolean isNeedExecute() {
        return (getBlockQueue() != null && !getBlockQueue().isEmpty());
    }

    protected abstract BlockingQueue<T> getBlockQueue();

    protected void initBlockQueue(){
        if (blockQueue == null) {
            this.blockQueue = getBlockQueue();
        }
    }
}
